#include <cstdio>
#include <algorithm>

using namespace std;

struct Student {
	int GE, GI, G;
	int r, id;
	int choice[6];
}stu[40010];

struct School {
	int quota;  // 招生额度
	int num;  // 实际招生人数
	int id[40010];  // 考生编号
	int last;  // 最后一个考生编号
}school[110];

bool cmp(Student a, Student b) {
	if (a.G != b.G) return a.G > b.G;
	else return a.GE > b.GE;
}

bool cmpID(int a, int b) {
	return stu[a].id < stu[b].id;
}

int main() {
	int n, m, k;
	scanf("%d%d%d", &n, &m, &k);
	for (int i = 0; i < m; i++) {
		scanf("%d", &school[i].quota);
		school[i].num = 0;
		school[i].last = -1;
	}
	for (int i = 0; i < n; i++) {
		stu[i].id = i;
		scanf("%d%d", &stu[i].GE, &stu[i].GI);
		stu[i].G = stu[i].GE + stu[i].GI;
		for (int j = 0; j < k; j++) {
			scanf("%d", &stu[i].choice[j]);
		}
	}

	sort(stu, stu+n, cmp);
	for (int i = 0; i < n; i++) {
		if (i != 0 && stu[i].G==stu[i-1].G && stu[i].GE==stu[i-1].GE) {
			stu[i].r = stu[i-1].r;
		} else {
			stu[i].r = i;
		}
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < k; j++) {
			int choice = stu[i].choice[j];
			int num = school[choice].num;
			int last = school[choice].last;

			if (num < school[choice].quota || (last != -1 && stu[i].r == stu[last].r)) {
				school[choice].id[num] = i;
				school[choice].last = i;
				school[choice].num++;
				break;
			}
		}
	}

	for (int i = 0; i < m; i++) {
		if (school[i].num > 0) {
			sort(school[i].id, school[i].id+school[i].num, cmpID);
			for (int j = 0; j < school[i].num; j++) {
				printf("%d", stu[school[i].id[j]].id);
				if (j < school[i].num-1) {
					printf(" ");
				}
			}
		}

		printf("\n");
	}

	return 0;
}











